Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Async function returning promise but console.log does give out the result as wanted | NodeJS Own Module

I just wrote a simple and small API to define the gender of a person. It works via a get-request to a page which gives me back a JSON object.

This is the code of my module:

'use strict';
const https = require('https');

exports.getGender = function (name) {
  return new Promise((resolve) => {
    //send request to api
    https.get(`https://api.genderize.io?name=${name}`, (resp) => {
      var data = '';
      //replace data
      resp.on('data', (chunk) => {
        data += chunk;
      });
      //define information on end of query
      resp.on('end', () => {
        var info = JSON.parse(data);
        //check accuracy of gender
        if (info.probability >= 0.8) {
          //check gender
          if (info.gender == 'male') {
            resolve('Sehr geehrter Herr');
          } else if (info.gender == 'female') {
            resolve('Sehr geehrte Frau');
          }
        } else {
          resolve('Sehr geehrte/r Frau / Herr');
        }
      });
    });
  });
};

And this is the call I do in the main file:

//get gender of person
async function getG(name) {
  const data = await genderApi.getGender(name);
  return data;
}

getG('Tim');

But the output is just Promise { <pending> }, If I replace the return data with a console.log(data) it just works fine and it shows me the output in the console but I really need it to return the value cause later on I use this function in a text back to the user

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

getG function is also async so you must use with await keyword or .then callback

const val = await getG('Tim');
// or
getG('Tim').then((val) => {
 console.log(val)
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!